Imports System
Imports System.Collections.Generic
Imports Cdm
Imports Cdm.AppServices
Imports Cdm.DataCore
Imports Cdm.DataCore.Entities
Imports Cdm.DataCore.Budget
Imports InputControls

Partial Class Admin_FMSATaxRate
    Inherits System.Web.UI.Page

#Region "CONSTANTS"
    Private Const _CLASS_NAME As String = "Admin_FMSATaxRate"
#End Region

#Region "PRIVATE METHODS"


    Private Function ValidateEffectiveDate(ByVal fmsaUserId As Integer, ByVal effectiveDate As DateTime) As Boolean

        Try

            Dim oFMSATaxRates As List(Of FMSATaxRate)
            Dim expirationDate As DateTime
            Dim lcv As Integer

            oFMSATaxRates = FMSATaxRate.ListByFMSAUserId(fmsaUserId)

            For lcv = 0 To oFMSATaxRates.Count - 1
                If effectiveDate < oFMSATaxRates(lcv).EffectiveDate Or effectiveDate < expirationDate Then
                    Return False
                    Exit Function
                End If
            Next

            Return True

        Catch ex As Exception
            Log.ErrorLog(_CLASS_NAME, "An error occurred in ExpireCurrentTaxRate() method.", ex)
            Throw ex
        End Try

    End Function

    Private Function ValidateEffectiveDate(ByVal fmsaUserId As Integer, ByVal effectiveDate As DateTime, ByVal fmsaTaxRateId As Integer) As Boolean

        Try

            Dim oFMSATaxRates As List(Of FMSATaxRate)
            Dim expirationDate As DateTime
            Dim lcv As Integer

            oFMSATaxRates = FMSATaxRate.ListByFMSAUserId(fmsaUserId)

            For lcv = 0 To oFMSATaxRates.Count - 1
                If oFMSATaxRates(lcv).FMSATaxRateId <> fmsaTaxRateId Then
                    If effectiveDate < oFMSATaxRates(lcv).EffectiveDate Or effectiveDate < expirationDate Then
                        Return False
                        Exit Function
                    End If
                End If
            Next

            Return True

        Catch ex As Exception
            Log.ErrorLog(_CLASS_NAME, "An error occurred in ExpireCurrentTaxRate() method.", ex)
            Throw ex
        End Try

    End Function

    Private Sub ExpireCurrentTaxRate(ByVal fmsaUserId As Integer, ByVal effectiveDate As DateTime)

        Try
            Dim oFMSATaxRate As FMSATaxRate

            Try
                oFMSATaxRate = FMSATaxRate.RetrieveMostCurrentByFmsaUserId(fmsaUserId)
            Catch ex As Exception
                oFMSATaxRate = Nothing
            End Try

            If oFMSATaxRate IsNot Nothing Then
                oFMSATaxRate.ExpirationDate = effectiveDate.AddDays(-1.0)
                oFMSATaxRate.LastModified = fmsaUserId
                oFMSATaxRate.DateModified = DateTime.Now
                oFMSATaxRate.Persist()
            End If

        Catch ex As Exception
            Log.ErrorLog(_CLASS_NAME, "An error occurred in ExpireCurrentTaxRate() method.", ex)
            Throw ex
        End Try

    End Sub

#End Region

#Region "EVENT HANDLERS"

    Protected Sub FMSATaxRateAddButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FMSATaxRateAddButton.Click

        Try

            Me.FMSATaxRatesGridView.Visible = False
            Me.FMSATaxRateAddButton.Visible = False
            Me.FMSATaxRateEditButton.Visible = False
            Me.ErrorSummaryLabel.Visible = False
            Me.FmsaTaxRateIdHiddenField.Value = String.Empty

            Me.SubmitButton.Text = "Submit"
            Me.FMSATaxRateDetailsView.Visible = True
            Me.SubmitButton.Visible = True
            Me.CancelButton.Visible = True

        Catch ex As Exception
            Log.ErrorLog(_CLASS_NAME, "An error occurred in SubmitButton_Click() method.", ex)
        End Try

    End Sub

    Protected Sub CancelButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CancelButton.Click

        Try

            Dim txtTaxRate As TextBox = CType(Me.FMSATaxRateDetailsView.FindControl("FMSATaxRateTextBox"), TextBox)
            Dim datEffectiveDate As DateInputControl = CType(Me.FMSATaxRateDetailsView.FindControl("EffectiveDateInputControl"), DateInputControl)

            Me.FMSATaxRatesGridView.Visible = True
            Me.FMSATaxRateAddButton.Visible = True
            Me.FMSATaxRateEditButton.Visible = True

            txtTaxRate.Text = String.Empty
            datEffectiveDate.Value = DateTime.MinValue
            Me.FmsaTaxRateIdHiddenField.Value = String.Empty

            Me.FMSATaxRateDetailsView.Visible = False
            Me.SubmitButton.Visible = False
            Me.CancelButton.Visible = False
            Me.ErrorSummaryLabel.Visible = False

        Catch ex As Exception
            Log.ErrorLog(_CLASS_NAME, "An error occurred in SubmitButton_Click() method.", ex)
        End Try

    End Sub

    Protected Sub SubmitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitButton.Click

        Try

            Dim txtTaxRate As TextBox = CType(Me.FMSATaxRateDetailsView.FindControl("FMSATaxRateTextBox"), TextBox)
            Dim datEffectiveDate As DateInputControl = CType(Me.FMSATaxRateDetailsView.FindControl("EffectiveDateInputControl"), DateInputControl)
            Dim oFMSAUser As FMSAUser
            Dim iUserId As Integer
            Dim iFmsaId As Integer
            Dim iFmsaTaxRateID As Integer
            Dim dTaxRate As Double = 0.0
            Dim effectiveDate As DateTime
            Dim oFMSATaxRate As FMSATaxRate

            iUserId = UserAuthorization.CurrentUser.UserId
            oFMSAUser = FMSAUser.RetrieveByUserId(iUserId)
            iFmsaId = oFMSAUser.FMSAId

            'VALIDATE TAX RATE
            If txtTaxRate.Text.Trim() = "" Then
                Me.ErrorSummaryLabel.Text = "You need to enter a value for Tax Rate before continuing."
                Me.ErrorSummaryLabel.Visible = True
                Exit Sub
            Else
                If Not IsNumeric(txtTaxRate.Text.Trim()) Then
                    Me.ErrorSummaryLabel.Text = "You need to enter a valid value for Tax Rate before continuing."
                    Me.ErrorSummaryLabel.Visible = True
                    Exit Sub
                End If

                Try
                    dTaxRate = CType(txtTaxRate.Text.Trim(), Double)
                Catch ex As Exception
                    Me.ErrorSummaryLabel.Text = "You need to enter a valid value for Tax Rate before continuing."
                    Me.ErrorSummaryLabel.Visible = True
                    Exit Sub
                End Try
            End If

            'VALIDATE THE EFFECTIVE DATE
            If datEffectiveDate.Value = "" Then
                Me.ErrorSummaryLabel.Text = "You need to select an Effective Date before continuing."
                Me.ErrorSummaryLabel.Visible = True
                Exit Sub
            Else
                effectiveDate = CType(datEffectiveDate.Value, DateTime)
            End If

            'ADD THE NEW FMSATaxRate
            If Me.SubmitButton.Text.ToUpper() = "UPDATE" Then

                iFmsaTaxRateID = CType(Me.FmsaTaxRateIdHiddenField.Value, Integer)

                'VALIDATE THE EFFECTIVE DATE
                If Not Me.ValidateEffectiveDate(iUserId, effectiveDate, iFmsaTaxRateID) Then
                    Me.ErrorSummaryLabel.Text = "You need to select an Effective Date that occurs after any previous Effective Dates or Expiration Dates before continuing."
                    Me.ErrorSummaryLabel.Visible = True
                    Exit Sub
                End If

                'GET THE CURRENT FMSATaxRate OBJECT
                oFMSATaxRate = FMSATaxRate.Retrieve(iFmsaTaxRateID)

            Else

                'VALIDATE THE EFFECTIVE DATE
                If Not Me.ValidateEffectiveDate(iUserId, effectiveDate) Then
                    Me.ErrorSummaryLabel.Text = "You need to select an Effective Date that occurs after any previous Effective Dates or Expiration Dates before continuing."
                    Me.ErrorSummaryLabel.Visible = True
                    Exit Sub
                End If

                'UPDATE EXPIRATION DATES FOR ALL EXISITING TaxRates FOR THIS FMSA
                Me.ExpireCurrentTaxRate(iUserId, CType(datEffectiveDate.Value, DateTime))

                'CREATE A NEW FMSATaxRate OBJECT
                oFMSATaxRate = New FMSATaxRate()

            End If

            oFMSATaxRate.FMSAId = iFmsaId
            oFMSATaxRate.TaxRate = dTaxRate
            oFMSATaxRate.EffectiveDate = effectiveDate
            oFMSATaxRate.DateModified = DateTime.Now
            oFMSATaxRate.LastModified = iUserId
            oFMSATaxRate.Persist()

            ''RETRIEVE A NEW DATASET
            Me.FMSATaxRatesGridView.DataBind()

            'HIDE THE INPUT FORM
            Me.SubmitButton.Visible = False
            Me.CancelButton.Visible = False
            Me.FMSATaxRateDetailsView.Visible = False

            'CLEAR THE INPUT FORM
            txtTaxRate.Text = String.Empty
            datEffectiveDate.Value = DateTime.MinValue
            Me.FmsaTaxRateIdHiddenField.Value = String.Empty

            'CLEAR THE ERROR MESSAGE
            If Me.ErrorSummaryLabel.Visible Then
                Me.ErrorSummaryLabel.Visible = False
            End If

            'DISPLAY THE GRIDVIEW
            Me.FMSATaxRatesGridView.Visible = True
            Me.FMSATaxRateAddButton.Visible = True
            Me.FMSATaxRateEditButton.Visible = True

        Catch ex As Exception
            Log.ErrorLog(_CLASS_NAME, "An error occurred in SubmitButton_Click() method.", ex)
        End Try

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Not Me.IsPostBack Then

                Me.FMSATaxRateObjectDataSource.SelectParameters("FmsaUserID").DefaultValue = UserAuthorization.CurrentUser.UserId

            End If

        Catch ex As Exception

        End Try
    End Sub

    Protected Sub FMSATaxRatesGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles FMSATaxRatesGridView.RowDataBound

        Try
            If e.Row.DataItemIndex = 0 Then
                AccessibilityController.AddImageToSortableGridHeaders(Me.FMSATaxRatesGridView)
            End If

            Dim fieldName As TextBox = CType(e.Row.FindControl("FMSATaxRateTextBox"), TextBox)

            If Not fieldName Is Nothing Then
                SetFocus(fieldName.ClientID)
            End If

        Catch ex As Exception
            Log.ErrorLog(_CLASS_NAME, "An error occurred in FMSATaxRatesGridView_RowDataBound() method.", ex)
        End Try

    End Sub

    Protected Sub FMSATaxRateEditButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FMSATaxRateEditButton.Click

        Try
            Dim oFmsaTaxRate As FMSATaxRate
            Dim txtTaxRate As TextBox
            Dim datEffectiveDate As DateInputControl

            txtTaxRate = CType(Me.FMSATaxRateDetailsView.FindControl("FMSATaxRateTextBox"), TextBox)
            datEffectiveDate = CType(Me.FMSATaxRateDetailsView.FindControl("EffectiveDateInputControl"), DateInputControl)

            oFmsaTaxRate = FMSATaxRate.RetrieveMostCurrentByFmsaUserId(UserAuthorization.CurrentUser.UserId)
            txtTaxRate.Text = oFmsaTaxRate.TaxRate.ToString()
            datEffectiveDate.Value = oFmsaTaxRate.EffectiveDate.ToShortDateString()
            Me.FmsaTaxRateIdHiddenField.Value = oFmsaTaxRate.FMSATaxRateId

            Me.FMSATaxRatesGridView.Visible = False
            Me.FMSATaxRateAddButton.Visible = False
            Me.FMSATaxRateEditButton.Visible = False

            Me.SubmitButton.Text = "Update"
            Me.FMSATaxRateDetailsView.Visible = True
            Me.SubmitButton.Visible = True
            Me.CancelButton.Visible = True

        Catch ex As Exception
            Log.ErrorLog(_CLASS_NAME, "An error occurred in FMSATaxRateEditButton_Click() method.", ex)
        End Try

    End Sub

#End Region

End Class
